// 封装的方法
var util = require('../../../utils/util.js');
// 封装的API
var api = require('../../../config/api.js');
Page({
  data: {
    // 要上传的地址信息
    address: {
      mobile: '',
      trueName: '',
    },
    selectAddress:'',
    textValue: '',
    addressId: 0,
  },
  onLoad: function (options) {
    
  },
  onShow: function () {
    let addressID = wx.getStorageSync('addressId')
    if (addressID !== '' && addressID !== undefined) {
      this.setData({
        addressId: addressID
      })
      // 有addressID 获取指定的收货地址
      this.getAddressInfo(addressID)
    }
  },
  // 提交审核
  saveAddress() {
    let that = this;
    let address = that.data.address;
    let selectAddress=that.data.selectAddress;
    let note = that.data.textValue;
    if (address.trueName == '' || address.trueName == undefined) {
      util.showErrorToast('请输入姓名');
      return false;
    }
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1})|(16[0-9]{1})|(19[0-9]{1}))+\d{8})$/;
    if (address.mobile.length == 0) {
      util.showErrorToast("手机号不得为空")
        return false;
    } else if (address.mobile.length < 11) {
      util.showErrorToast("手机号格式错误")
        return false;
    } else if (!myreg.test(address.mobile)) {
      util.showErrorToast("手机号有误")
        return false;
    }
    if (selectAddress == '' || that.data.addressId == 0) {
      util.showErrorToast('请选择地址');
      return false;
    }
    if (note == '' || note == undefined) {
      util.showErrorToast('请输入申请理由');
      return false;
    }
    let userID = wx.getStorageSync('userID');
    util.requestApi(api.teamApply, {
      userID: userID,
      userName: address.trueName,
      mobile: address.mobile,
      addressID: that.data.addressId,
      note: note,
    }).then(function (res) {
      if (res.code == 200) {
        util.showErrorToast('发送成功，请耐心等待审核！');
        wx.navigateBack();
      }
    });
  },
  // 输入手机号
  mobilechange(e) {
    let mobile = e.detail.value;
    let address = this.data.address;
    address.mobile=mobile;
    if (util.testMobile(mobile)) {
      address.mobile = mobile;
      this.setData({
        address: address
      });
    }
  },
  // 输入姓名
  bindinputName(event) {
    let address = this.data.address;
    address.trueName = event.detail.value;
    this.setData({
      address: address
    });
  },
  // 选择地址
  toSelectAddress: function () {
    wx.navigateTo({
      url: '/pages/ucenter/address/index?type=1',
    });
  },
  // 输入申请理由
  bindTextValue(event) {
    this.setData({
      textValue: event.detail.value
    })
  },
  // 根据地址ID获取选中的地址信息
  getAddressInfo(id) {
    let that = this;
    util.requestApi(api.getAddressInfo, {
      addressID: id
    }).then(function (res) {
      if (res.code == 200) {
        that.setData({
          selectAddress: res.content.cityName+res.content.areaName+res.content.addressInfo
        })
      }
      wx.removeStorageSync('addressId')
    })
  },
})